package com.quo.controller;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.quo.dto.QuotesDto;
import com.quo.service.QuoteService;
import com.quo.utils.DownloadUtils;
import com.quo.utils.ExcelWriter;
import com.alibaba.fastjson.JSON;

/** 

* @author zhoumin

*/
@RestController("quoteScheduledController")
@EnableScheduling
@RequestMapping(value="/api")
public class QuoteScheduledController {
	
	@Autowired
	public QuoteService qService;	
	
	@Autowired
    public HttpServletResponse request;
    
    @Autowired
    public HttpServletResponse response;
	
	//导出报价单信息
	  //@Scheduled(cron = "0/10 * * * * ?")
	  public void QuoteExportList() {
		  
		  //创建FileOutputStream对象
	        String filePath="H:/list.xml";
	        FileOutputStream fileOutputStream=null;
	        
	        List<QuotesDto> qlist = qService.getQuoteList(); 
	        String qlistjason = JSON.toJSONString(qlist);
	        try {
	            //得到FileOutputStream对象
	            //注：new FileOutputStream(filePath)的创建方式，当写入内容时会覆盖原来的内容
	            //new FileOutputStream(filePath，true)的创建方式，当写入内容时是追加到文件后面
	            fileOutputStream = new FileOutputStream(filePath);
	            //写入字符串
	            
	            fileOutputStream.write(qlistjason.getBytes(),0,qlistjason.length());
	        } catch (IOException e) {
	            e.printStackTrace();
	        }finally {
	            try {
	                fileOutputStream.close();
	            } catch (IOException e) {
	                e.printStackTrace();
	            }

	        }
			System.out.println("执行定时任务 " + LocalDateTime.now());
	    }

		
		@GetMapping("/quotelist")
	    //@Scheduled(cron = "0/10 * * * * ?")
	    public void exportData2Excel(){

	    	List<QuotesDto> qlist = qService.getQuoteList();  
	    	
	    	System.out.println("qlist"+qlist);
	             //1.通过读取模板创建工作簿
	             Workbook wb = new XSSFWorkbook();
			
	             //2.创建sheet
	             Sheet sheet = wb.createSheet();
	             //3.提取正文的样式
	             String[] titles = "件名,会社名,申請者,作成期日,番号,状態,金額".split(",");
	             Row row = sheet.createRow(0);//具体根据excel中的模板决定到底需要从第几行开始录入数据
	             int titleIndex = 0;
	            
	             for (String title : titles) {
	            	
	                 Cell cell = row.createCell(titleIndex++);
	                 cell.setCellValue(title);
	                 
	                             
	             }
	            
	             int rowIndex = 1;
	             Cell cell = null;
	             for (QuotesDto QuotesDto : qlist) {
	            	 row = sheet.createRow(rowIndex++);
	            	 cell = row.createCell(0);
	            	 cell.setCellValue(QuotesDto.getAbst());
	            	 cell = row.createCell(1);
	            	 cell.setCellValue(QuotesDto.getCname());
	            	 cell = row.createCell(2);
	            	 cell.setCellValue(QuotesDto.getEname());
	            	 cell = row.createCell(3);
	            	 cell.setCellValue(QuotesDto.getQdate());
	            	 cell = row.createCell(4);
	            	 cell.setCellValue(QuotesDto.getQid());
	            	 cell = row.createCell(5);
	            	 cell.setCellValue(QuotesDto.getQsname());
	            	 cell = row.createCell(6);
	            	 cell.setCellValue(QuotesDto.getTotalAmount());
	             }
	             
	             ByteArrayOutputStream os = new ByteArrayOutputStream();
	             try {
					wb.write(os);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	             try {
					new DownloadUtils().download(os,response,"quotelist.xlsx");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	             System.out.println("执行定时任务 " + LocalDateTime.now());
	 }    
	    
	    
	    

	            
	  	
	    	            
}
